package com.wyzz.hr.action;

import com.opensymphony.xwork2.Action;
import com.wyzz.hr.base.Constants;
import com.wyzz.hr.base.FileOperate;
import com.wyzz.hr.bo.LogBo;
import com.wyzz.hr.pojo.Userinfo;
import com.wyzz.hr.spring.SpringBeanFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

public class LoginOutAction implements Action, Constants {

	public LoginOutAction() {
	}

	public String execute() throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();
		Userinfo user = (Userinfo) session.getAttribute("userinfo");
		String userNo = null;
		String ip = null;
		if (user != null) {
			userNo = user.getUiUsername();
			ip = user.getUiLastLoginIp();
		}
		session.setAttribute("authList", null);
		session.setAttribute("userinfo", null);
		session.removeAttribute("authList");
		session.removeAttribute("userinfo");
		session.removeAttribute("requiredURL");
		writeLoggers(userNo, ip, (String) session.getAttribute("clientNo"));
		session.setAttribute("loginModel", null);
		session.removeAttribute("loginModel");
		session.setAttribute("clientNo", null);
		session.removeAttribute("clientNo");
		session.invalidate();
		return "login";
	}

	private void writeLoggers(String userNo, String ip, String clientNo) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
		String info = (new StringBuilder()).append(
				"\u767B\u51FA\u65F6\u95F4\uFF1A").append(df.format(new Date()))
				.append("  \u5BA2\u6237\u7F16\u53F7\uFF1A").append(clientNo)
				.append("  \u7528\u6237\u540D\uFF1A").append(userNo).append(
						"  \u767B\u51FAIP\uFF1A").append(ip).toString();
		LogBo logBo = (LogBo) SpringBeanFactory.getBean("logmanager");
		logBo.updateXML((new StringBuilder()).append(
				FileOperate.getFileHomePath()).append("login_log/login.xml")
				.toString(), (new StringBuilder()).append(info).append("\n")
				.toString());
	}

	static Logger logger = Logger.getLogger(LoginOutAction.class);

}
